//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <string.h>
#include <elatypes.h>

wchar_t* __cdecl wcsncpy(wchar_t* pDest, const wchar_t* pSource, size_t count)
{
#ifdef _GNUC
#if defined(_x86)
    int temp0, temp1, temp2, temp3;
    ASM(
        "1:"
        "decl %2;"
        "js 2f;"
        "lodsw;"
        "stosw;"
        "testw %%ax,%%ax;"
        "jne 1b;"
        "rep;"
        "stosw;"
        "2:"
        :"=D" (temp0), "=S" (temp1),  "=c" (temp2), "=a" (temp3)
        :"0" (pDest), "1" (pSource), "2" (count)
        :"memory");
//#elif defined(_arm)
//    asm __volatile__(
//        "1:\tcmp r2, #0\n\t"
//        "ble 2f\n\t"
//        "ldrh r3, [r1], #2\n\t"
//        "strh r3, [r0], #2\n\t"
//        "sub r2, r2, #1\n\t"
//        "mov r2, r2, lsl #16\n\t"
//  "mov r2, r2, lsr #16\n\t"
//        "bne 1b\n"
//        "2:"
//        :"=r"(pDest)
//        ::"r3");
//#endif

    return pDest;
#endif  //if defined(_x86)

#endif    //ifdef _GNUC

    wchar_t* p = pDest;

    while (count > 0 && (*p++= *pSource++) != L'\0')
        count--;

    if (count > 0)
        while(--count > 0) *p++= L'\0';        // padded with null characters

    return pDest;
}
